import { defineStore } from 'pinia'
import useEventBus from '@/use/use-event-bus'

// 聊天内容收缩宽度
export const CHAT_CONTENT_FOLD_WIDTH = 632
// 频道列表最小宽度
export const CHANNEL_LIST_MIN_WIDTH = 82
// 频道列表最大宽度
export const CHANNEL_LIST_MAX_WIDTH = 270
// AI助手最小宽度
export const ASSISTANT_MIN_WIDTH = 415
// 展示待办时AI助手最小宽度
export const PERSONAL_TO_DO_MIN_WIDTH = 750

const useFold = defineStore('fold', () => {
  let mainPartition = null
  let chatPartition = null
  const hideChatContent = ref(true)
  const hideAssistant = ref(false)
  const rootWidth = ref(true)
  const rootPartitionValue = ref(0)
  const chatWidth = ref(null)
  const chatPartitionValue = ref(1)
  const chatContentWidth = ref(null)
  const assistantWidth = ref(null)

  const eventBus = useEventBus()

  function setRootPartitionValue(v) {
    rootPartitionValue.value = v
  }

  function setChatPartitionValue(v) {
    chatPartitionValue.value = v
  }

  function setMainPartition(partition) {
    mainPartition = partition
  }

  function setChatPartition(partition) {
    chatPartition = partition
  }

  function setRootWidth(width) {
    rootWidth.value = width
  }

  function setChatWidth(width) {
    chatWidth.value = width
  }

  function setChatContentWidth(width) {
    chatContentWidth.value = width
  }

  function setAssistantWidth(width) {
    assistantWidth.value = width
  }

  function setHideChatContent(v) {
    hideChatContent.value = v
    if (v) {
      mainPartition.value.flexible('leftMinWidth')
      hideAssistant.value && setHideAssistant(false)
    } else {
      // 如果宽度不足以展示聊天和AI助手时，隐藏AI助手
      if (rootWidth.value <= CHAT_CONTENT_FOLD_WIDTH + PERSONAL_TO_DO_MIN_WIDTH) {
        if (rootWidth.value <= CHANNEL_LIST_MAX_WIDTH + PERSONAL_TO_DO_MIN_WIDTH) {
          !hideAssistant.value && setHideAssistant(true)
        }
        if (!hideAssistant.value) {
          mainPartition.value.flexible('rightMinWidth')
        }
        chatPartition.value.flexible('leftMaxWidth')
      } else {
        if (hideAssistant.value) {
          chatPartition.value.flexible('leftMaxWidth')
        } else {
          mainPartition.value.setContainer2Size(PERSONAL_TO_DO_MIN_WIDTH)
          chatPartition.value.flexible('leftMaxWidth')
        }
      }
    }
  }

  function setHideAssistant(v) {
    hideAssistant.value = v
    if (v) {
      mainPartition.value.flexible('leftMaxWidth')
      if (hideChatContent.value) {
        hideChatContent.value && setHideChatContent(false)
        eventBus.emit('dialogScrollBottom')
      }
    } else {
      // 如果宽度不足以展示聊天和AI助手时，隐藏聊天
      if (rootWidth.value <= CHANNEL_LIST_MAX_WIDTH + PERSONAL_TO_DO_MIN_WIDTH) {
        !hideChatContent.value && setHideChatContent(true)
      } else {
        if (hideChatContent.value) {
          mainPartition.value.flexible('leftMinWidth')
        } else {
          mainPartition.value.flexible('rightMinWidth')
        }
      }
    }
  }

  return {
    hideChatContent: readonly(hideChatContent),
    hideAssistant: readonly(hideAssistant),
    rootWidth: readonly(rootWidth),
    rootPartitionValue: readonly(rootPartitionValue),
    chatPartitionValue: readonly(chatPartitionValue),
    setRootWidth,
    setChatWidth,
    setChatContentWidth,
    setAssistantWidth,
    setMainPartition,
    setChatPartition,
    setHideChatContent,
    setHideAssistant,
    setRootPartitionValue,
    setChatPartitionValue,
  }
})

export default useFold
